<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="mydiv"></div>
    <input type="text" id="txt">
    <script>

        var obj={};//定义一个完成数据劫持的承载的对象
        var msg="嘻嘻";//定义一个依赖变量
        var mydiv=document.getElementById("mydiv");//DOM对象
        var txt=document.getElementById("txt");//DOM对象
        Object.defineProperty(obj,"message",{
            get(){
                console.log("get");
                return msg;
                
            },
            set(val){
                msg=val;
                mydiv.innerHTML=val;
                console.log("set");
            }   
        });
        mydiv.innerHTML=obj.message;
        txt.value=obj.message;
        txt.oninput=function(){
            obj.message=this.value;//触发set方法
        };

    </script>
</body>
</html>